package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.*;


import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//套餐
@Service
@Transactional
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;


    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;


    //套餐列表
    @Override
    public Page<Setmeal> findByPage(Integer pageNum, Integer pageSize,String name) {
        //1-套餐条件分页查询
        //1-1构建条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotBlank(name),Setmeal::getName,name);
        //1-2开启分页
        Page<Setmeal> page = new Page<>(pageNum, pageSize);
        //1-3mapper查询
        page = setmealMapper.selectPage(page, qw);

        //遍历套餐列表
        for (Setmeal setmeal : page.getRecords()) {
            //2.根据category_id 查询并封装分类名称
            Category category = categoryMapper.selectById(setmeal.getCategoryId());
            //3.根据id查询并封装菜品列表
            LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
            List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper);
            setmeal.setSetmealDishes(setmealDishes);
        }
        //返回组装后的数据
        return page;
    }

    //新增
    @Override
    public void save(Setmeal setmeal) {
        //添加基本信息
        setmealMapper.insert(setmeal);
        log.info("套餐主键返回:"+setmeal.getImage());
        //2.遍历前端提交套餐菜品列表
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            //关联套餐id
            setmealDish.setSetmealId(setmeal.getId());
            //保存中间表
            setmealDishMapper.insert(setmealDish);
        }
    }

    //删除
    @Override
    public void deleteBatch(List<Long> ids) {
        //1.先查询数据状态   select count(*) from setmeal where status=1 and id in(1,3,5);
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId,ids);// id in(1,3,5);
        qw.eq(Setmeal::getStatus,1);  //status=1
        Integer count = setmealMapper.selectCount(qw);
        if (count>0){
            throw new CustomException("套餐中有启动状态，请停售在删除~~");
        }

        //2.删套餐
        setmealMapper.deleteBatchIds(ids);

        //3.删除套餐内菜品中间表
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishMapper.delete(wrapper);

    }

    //回显
    @Override
    public Setmeal findById(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);

        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(qw);
        //设置套餐返回
        setmeal.setSetmealDishes(setmealDishes);
        //结果返回
        return setmeal;
    }

    //修改
    @Override
    public void updateById(Setmeal setmeal) {
        //1.首先更改基本信息
        setmealMapper.updateById(setmeal);
        //2.构建条件并根据id删除套餐菜品列表中的菜品
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,setmeal.getId());
        setmealDishMapper.delete(qw);
        //3.获取新菜品中的菜品集合
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        //4.遍历集合并添加菜品信息
        for (SetmealDish setmealDish : setmealDishes) {
            //3.1将套餐菜品中的菜品设置套餐id
            setmealDish.setSetmealId(setmeal.getId());
            //3.2添加菜品到套餐菜品表中
            setmealDishMapper.insert(setmealDish);
        }
    }

    //起售/停售
    @Override
    public void updateSetmealStatusById(Integer status, List<Long> ids) {
        //1.构建条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId,ids);
        //修改实体类
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        //调用mapper
        setmealMapper.update(setmeal,qw);
    }

    //套餐列表
    @Override
    public List<Setmeal> findList(Long categoryId, Integer status) {
        //构建条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId,categoryId);
        qw.eq(Setmeal::getStatus,status);

        //执行并返回
        return setmealMapper.selectList(qw);
    }

    //套餐展示菜品
    @Override
    public List<Dish> findDishBySetmealId(Long setmealId) {
        //构建条件
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,setmealId);//根据id查询套餐
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(qw);

        //创建一个空集合
        List<Dish>dishList = new ArrayList<>();
        for (SetmealDish setmealDish : setmealDishes) {
            Integer copies = setmealDish.getCopies();//获取份数
            Long dishId = setmealDish.getDishId();//获取菜品id
            Dish dish = dishMapper.selectById(dishId);

            dish.setCopies(copies);
            dishList.add(dish);

        }
        return dishList;


    }


}
